/**************************************************************************
	FSHOME - OptoIn I2C Handle Node - Peer eth
***************************************************************************/
#include "bconf/DINo_v2_Peer.h"				// Load QuickCfg.h configuration parameters automatically
#include "bconf/DINo_v2_DisableMACRAW.h"

// #define SKECTH_VNET_MACACO_DEBUG
#ifdef SKECTH_VNET_MACACO_DEBUG
  #define VNET_DEBUG_INSKETCH
  #define VNET_DEBUG    1
  #define MaCaco_DEBUG_INSKETCH
  #define MaCaco_DEBUG  1
#endif

#include "Souliss.h"
#include "SpeakEasy.h"						// Is a library to code easy Souliss examples
#include <SPI.h>

// #define SKETCH_DEBUG  1

// Include common definitions
#include "DINo_v2_HW_Setup.h"			// standard board hw configuration
#include "FS_Souliss_Network.h"			// standard board hw configuration

// I2C Software support
// Define the hw pin that will be used by the SoftI2CMaster library
// that require their definition in port/pin form.

// Analog 0 and 1 are free pin available on proDINo P1 conector
#define SDA_PORT PORTF
#define SDA_PIN 7    // Analog 0 - Connector P1 - Pin 8
#define SCL_PORT PORTF
#define SCL_PIN 6    // Analog 1 - Connector P1 - Pin 6

#define I2C_TIMEOUT 100
#define I2C_NOINTERRUPT 0
#define I2C_SLOWMODE 1
#define FAC 1
#define I2C_CPUFREQ (F_CPU/FAC)

#include <SoftI2CMaster.h>

// SLOT definitions
#define IN_1_SLOT			0
#define IN_2_SLOT			1
#define IN_3_SLOT			2
#define IN_4_SLOT			3
#define IN_5_SLOT			4
#define IN_6_SLOT			5
#define IN_7_SLOT			6
#define IN_8_SLOT			7

// Global Variables

// Network address
uint8_t ip_address[4]  = {IP_ADDRESS_1, IP_ADDRESS_2, IP_ADDRESS_3, IP_ADDRESS_4_03};

#define I2C_MAX_ADDRESSES	1
//uint8_t I2C_addresses[I2C_MAX_ADDRESSES]  = {0x41, 0x43}; // Address with READ bit
uint8_t I2C_addresses[I2C_MAX_ADDRESSES]  = {0x41}; // Address with READ bit
uint8_t i2c_current_board = 0;

void setup()
{
	#ifdef SKETCH_DEBUG
	Serial.begin(9600);
	#endif

	// Init the board
	DINo_v2_HW_Setup();
	
	// Setup network configuration
	Souliss_SetIPAddress(ip_address, subnet_mask, ip_gateway);

	// Set Up the slots
	Souliss_SetT13(memory_map, IN_1_SLOT);
	Souliss_SetT13(memory_map, IN_2_SLOT);
	Souliss_SetT13(memory_map, IN_3_SLOT);
	Souliss_SetT13(memory_map, IN_4_SLOT);
	Souliss_SetT13(memory_map, IN_5_SLOT);
	Souliss_SetT13(memory_map, IN_6_SLOT);
	Souliss_SetT13(memory_map, IN_7_SLOT);
	Souliss_SetT13(memory_map, IN_8_SLOT);

	if (!i2c_init()) 
		TurnOnLED();
	else
		TurnOffLED();

} 

void loop()
{ 
	EXECUTEFAST() {						
		UPDATEFAST();	

		FAST_210ms() {
			#ifdef SKETCH_DEBUG
			Serial.print("Start: "); Serial.print(millis()); //Serial.print("\r\n");
			#endif

			uint8_t	value;
			uint8_t	slot;
			
			if(i2c_current_board == I2C_MAX_ADDRESSES)
				i2c_current_board = 0;
				
				#ifdef SKETCH_DEBUG
				Serial.print("Start reading: i2c_current_board: "); Serial.print(i2c_current_board, DEC); Serial.print(" - ");
				Serial.print("I2C_addresses[i2c_current_board]: 0x"); Serial.print(I2C_addresses[i2c_current_board], HEX); Serial.print("\n\r");
				#endif

			if (i2c_start(I2C_addresses[i2c_current_board])) {
				value = i2c_read(true);
				i2c_stop();

				#ifdef SKETCH_DEBUG
				Serial.print("Read: i2c_current_board: "); Serial.print(i2c_current_board, DEC); Serial.print(" - "); Serial.print("value: 0x"); Serial.print(value, HEX); Serial.print("\n\r");
				#endif
			
				for(slot=i2c_current_board*8; slot<(i2c_current_board+1)*8; slot++) {
					uint8_t b = value&0x01;
					#ifdef SKETCH_DEBUG
					Serial.print("Assigning state slot: ");	Serial.print(slot, DEC); Serial.print(" - "); Serial.print("state: "); Serial.print(b, DEC); Serial.print("\r\n");
					#endif
					if(value&0x01)
						Souliss_Input(memory_map, slot) = Souliss_T1n_OnCmd;
					else
						Souliss_Input(memory_map, slot) = Souliss_T1n_OffCmd;

					Souliss_Logic_T13(memory_map, slot, &data_changed);
					value = value >> 1;
				}				
			}
			i2c_current_board++;

			#ifdef SKETCH_DEBUG
			Serial.print("Stop: "); Serial.print(millis()); Serial.print("\r\n");
			#endif
		} 

		// Here we process all communication with other nodes
		FAST_PeerComms();
		
		// At first runs, we look for a gateway to join
		START_PeerJoin();
		
		// Periodically check if the peer node has joined the gateway
		FAST_1110ms() {
			if(JoinInProgress())	// If join is in progress, toogle the LED at every turn
				ToogleLED();
			else
				TurnOnLED();		// Once completed, turn ON
		}		
		
	}
	
	EXECUTESLOW() {	
		UPDATESLOW();

		// Here we periodically check for a gateway to join
		SLOW_PeerJoin();		
	}
} 
